package com.lightsaber.trade.core.entity.account.nopersist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class HomeMenuBar {
    private List<MenuItem> topLevelMenuItems = new ArrayList<MenuItem>();

    public void putTopLevelMenuItem(final MenuItem topLevelMenuItem) {
        topLevelMenuItems.add(topLevelMenuItem);
    }

    
    @Override
    public String toString() {
        StringBuilder htmlBuf = new StringBuilder();

        htmlBuf.append(" <div style='padding:5px'><table width='100%' border='0' cellspacing='25' cellpadding='0'>");

        // Get root context path from request URL
        HttpServletRequest request = ServletActionContext.getRequest();
        String rootContext = request.getContextPath();

        recursionMenuItem(topLevelMenuItems, htmlBuf, rootContext, 0);

        htmlBuf.append("</table></div>");
        return htmlBuf.toString();
    }

    public void recursionMenuItem(final List<MenuItem> menuItems, final StringBuilder htmlBuf,
            final String rootContext, int countItemPerRow) {

        Collections.sort(menuItems);

        for (MenuItem menuItem : menuItems) {
            if (menuItem.isLeaf() && menuItem.getTopMenuPos() == null) {
                htmlBuf.append("<li><a href='" + rootContext + "/"
                        + MenuBar.getLink(menuItem.getHref(), menuItem.getId()) + "' > ");
                htmlBuf.append(menuItem.getLabel());
                htmlBuf.append("</a></li>");
            } else if (!menuItem.isLeaf() && menuItem.getTopMenuPos() == null) {
                recursionMenuItem(menuItem.getChildren(), htmlBuf, rootContext, countItemPerRow);
            } else {

                int tempCountItemPerRow = countItemPerRow;
                if (tempCountItemPerRow == 0) {
                    htmlBuf.append("<tr>");
                }
                countItemPerRow = countItemPerRow + 1;
                if (countItemPerRow == 2) {
                    countItemPerRow = 0;
                    // tempCountItemPerRow=100 express ：row is full（have two
                    // items per row）must add tag end </tr>
                    tempCountItemPerRow = 100;
                }
                htmlBuf.append("<td style='width:100px;; vertical-align:top'><img src='../images/icon_"
                        + menuItem.getTopMenuPos()
                        + ".jpg' title='"
                        + menuItem.getLabel()
                        + "' /></td>");
                htmlBuf.append("<td style='width:280px; vertical-align:top'><ul class='normalUL sUL'>");
                if (menuItem.isLeaf()) {
                    htmlBuf.append("<li><a href='" + rootContext + "/"
                            + MenuBar.getLink(menuItem.getHref(), menuItem.getId()) + "' > ");
                    htmlBuf.append(menuItem.getLabel());
                    htmlBuf.append("</a></li>");
                } else {
                    recursionMenuItem(menuItem.getChildren(), htmlBuf, rootContext, countItemPerRow);
                }
                htmlBuf.append("</ul></td>");
                if (tempCountItemPerRow == 100) {
                    htmlBuf.append("</tr>");
                }

            }
        }

    }

}
